package ninesky.ContentTemp.service;

import java.io.IOException;
import java.util.Random;

import ninesky.ContentTemp.service.IMusicService.Stub;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.IBinder;
import android.os.RemoteException;

public class MusicService extends Service {

	private MediaPlayer mPlayer;

	public static final String PLAYSTATE_CHANGED = "ninesky.contenttemp.playstate_changed";
	public static final String META_CHANGED = "ninesky.contenttemp.meta_changed";

	private int mDuartion = 0;

	private final IMusicService.Stub mBinder = new Stub() {

		@Override
		public void play() throws RemoteException {
			// TODO play music
			if (!mPlayer.isPlaying()) {
				mPlayer.start();
				sendBroadcast(new Intent(PLAYSTATE_CHANGED));
			}
		}

		@Override
		public void pause() throws RemoteException {
			if (mPlayer.isPlaying()) {
				mPlayer.pause();
				sendBroadcast(new Intent(PLAYSTATE_CHANGED));
			}
		}

		@Override
		public long position() throws RemoteException {
			if (mPlayer != null) {
				return mPlayer.getCurrentPosition();
			}
			return 0;
		}

		@Override
		public long duartion() throws RemoteException {
			return mDuartion;
		}

		@Override
		public boolean isPlay() throws RemoteException {
			return mPlayer != null && mPlayer.isPlaying();
		}

		@Override
		public String getInfo() throws RemoteException {
			return "song" + String.valueOf(new Random().nextInt());
		}

		@Override
		public void playPause() throws RemoteException {
			if (mPlayer.isPlaying()) {
				mPlayer.pause();
			} else {
				mPlayer.start();
			}
			sendBroadcast(new Intent(PLAYSTATE_CHANGED));
		}
	};

	public void onCreate() {
		final String url = "http://www.oneleggedballerina.com/mp3s/one.mp3";
		mPlayer = new MediaPlayer();
		mPlayer.setOnPreparedListener(new OnPreparedListener() {

			@Override
			public void onPrepared(MediaPlayer mp) {
				mPlayer.start();
				mDuartion = mPlayer.getDuration();
				sendBroadcast(new Intent(META_CHANGED));

			}
		});

		mPlayer.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				try {
					mPlayer.reset();
					mPlayer.setDataSource(url);
					mPlayer.prepareAsync();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});

		try {
			mPlayer.setDataSource(url);
			mPlayer.prepareAsync();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	};

	@Override
	public IBinder onBind(Intent arg0) {
		return mBinder;
	}

}
